package com.jp.vn.CustomAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.vn.main.R;
import com.jp.vn.Object.Alphabet;

public class LearnAlphabetAdapter extends PagerAdapter {

	Context context;
	public LayoutInflater inflater;
	ArrayList<Alphabet> arrayAlphabet;
	static int i;
	public static int index = 0;
	Alphabet alphabet;
	public MediaPlayer mediaPlayer = null;
	String folder;

	public LearnAlphabetAdapter(Context context,ArrayList<Alphabet> arrayAlphabet, int type) {
		this.context = context;
		this.arrayAlphabet = arrayAlphabet;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mediaPlayer = new MediaPlayer();
		
		if(type == 1){
			folder = "hiragana";
		} else{
			folder = "katakana";
		}
	}

	@Override
	public int getCount() {
		return arrayAlphabet.size() / 5;
	}

	class ViewHolder {
		TextView text_alpha_1, text_alpha_2, text_alpha_3, text_alpha_4,
				text_alpha_5;
		TextView text_latin_1, text_latin_2, text_latin_3, text_latin_4,
				text_latin_5;
		ImageView imgAlpha;
		ImageView audio;
		String text_selected;
		LinearLayout alpha_layout;
	}

	@Override
	public Object instantiateItem(View container, final int position) {
		final ViewHolder holder;
		View layout = null;

		holder = new ViewHolder();
		layout = inflater.inflate(R.layout.item_alphabet, null);

		holder.text_alpha_1 = (TextView) layout.findViewById(R.id.text_alpha_1);
		holder.text_alpha_2 = (TextView) layout.findViewById(R.id.text_alpha_2);
		holder.text_alpha_3 = (TextView) layout.findViewById(R.id.text_alpha_3);
		holder.text_alpha_4 = (TextView) layout.findViewById(R.id.text_alpha_4);
		holder.text_alpha_5 = (TextView) layout.findViewById(R.id.text_alpha_5);

		holder.text_latin_1 = (TextView) layout.findViewById(R.id.text_latin_1);
		holder.text_latin_2 = (TextView) layout.findViewById(R.id.text_latin_2);
		holder.text_latin_3 = (TextView) layout.findViewById(R.id.text_latin_3);
		holder.text_latin_4 = (TextView) layout.findViewById(R.id.text_latin_4);
		holder.text_latin_5 = (TextView) layout.findViewById(R.id.text_latin_5);
		holder.alpha_layout = (LinearLayout) layout.findViewById(R.id.alpha_layout);

		holder.imgAlpha = (ImageView) layout.findViewById(R.id.imgChuCai);
		holder.audio = (ImageView) layout.findViewById(R.id.audio);
		
		final TextView[] tfAlpha = { holder.text_alpha_1, holder.text_alpha_2,
				holder.text_alpha_3, holder.text_alpha_4, holder.text_alpha_5 };

		final TextView[] tfLatin = { holder.text_latin_1, holder.text_latin_2,
				holder.text_latin_3, holder.text_latin_4, holder.text_latin_5 };

		int start = 0;
		
		if (position > 0) {
			start = position * 5;
		}

		int end = start + 5;

		if (index > 4) {
			index = 0;
		}

		for (int j = start; j < end; j++) {
			alphabet = arrayAlphabet.get(j);
			tfAlpha[index].setText(alphabet.getLetter());
			tfLatin[index].setText(alphabet.getMeaning());
			index++;
		}
		
		holder.text_selected = tfLatin[0].getText().toString();
		displayImg(tfLatin[0].getText().toString(), holder.imgAlpha, folder);

		tfAlpha[0].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayImg(tfLatin[0].getText().toString(), holder.imgAlpha, folder);
				holder.text_selected = tfLatin[0].getText().toString();
				
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}				
			}
		});

		tfAlpha[1].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayImg(tfLatin[1].getText().toString(), holder.imgAlpha,folder);
				holder.text_selected = tfLatin[1].getText().toString();
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}		
			}
		});

		tfAlpha[2].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayImg(tfLatin[2].getText().toString(), holder.imgAlpha, folder);
				holder.text_selected = tfLatin[2].getText().toString();
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}		
			}
		});

		tfAlpha[3].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayImg(tfLatin[3].getText().toString(), holder.imgAlpha, folder);
				holder.text_selected = tfLatin[3].getText().toString();
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}		
			}
		});

		tfAlpha[4].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayImg(tfLatin[4].getText().toString(), holder.imgAlpha, folder);
				holder.text_selected = tfLatin[4].getText().toString();
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}	
			}
		});

		holder.audio.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					stopPlaying();
					playSound(holder.text_selected);
				} catch (IOException e) {
					e.printStackTrace();
				}		
			}
		});

		((ViewPager) container).addView(layout);

		return layout;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	public void playSound(String fileName) throws IOException {
		AssetFileDescriptor afd = context.getAssets().openFd("hiragana_voice/" + fileName + ".ogg");
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
		mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {				
				mp.start();
			}
		});
		
		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.release();
				mediaPlayer = null;
			}
		});
		
		mediaPlayer.prepareAsync();
	}
	
	public void stopPlaying(){
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
	
	public void displayImg(String imgName,ImageView img, String folder){
		try {
			InputStream ims = context.getAssets().open(folder+"/"+imgName+".jpg");
			Drawable d = Drawable.createFromStream(ims, null);
            img.setImageDrawable(d);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
